//========================================================================
// *作者：海盗猫  主页：ilplay.com
// *脚本：UIFrameViewBehavior
// *描述：框架显示的逻辑脚本
// *?2021 MYJL . All rights reserved.
//* ======================================================================
using System;
using System.Collections.Generic;
using UnityEngine;

namespace HDMCode.UIFrame
{
    [HDMEnum("窗口类型")]
    public enum WrapperType
    {
        [HDMEnumChildLabel("全屏(层级最低,自动背景)")] FullScreen,
        [HDMEnumChildLabel("弹窗(层级偏低,自动背景)")] Dialog,
        [HDMEnumChildLabel("通知(层级偏高,重复加载)")] inform,
        [HDMEnumChildLabel("固定(层级最高,不可销毁)")] Fixation,
    }

    [HDMEnum("显示类型")]
    public enum ShowType
    {
        [HDMEnumChildLabel("标准")] Standard,
        [HDMEnumChildLabel("焦点")] Focussing,
        [HDMEnumChildLabel("队列")] Queue
    }

    [HDMEnum("穿透类型")]
    public enum PierceType
    {
        [HDMEnumChildLabel("可以穿透")] Penetrate,
        [HDMEnumChildLabel("不可穿透")] Impenetrate,
    }

    [HDMEnum("背景类型")]
    public enum BackGroundType
    {
        [HDMEnumChildLabel("透明背景")] Transparent,
        [HDMEnumChildLabel("半透明背景")] Translucent,
        [HDMEnumChildLabel("不透明背景")] Opaque,
        [HDMEnumChildLabel("自定义背景")] Custom
    }

    public class UIFrameViewBehavior : MonoBehaviour
    {
        /// <summary>
        /// UI加载接口方法
        /// </summary>
        private IUIFrameViewAssets uiAssets = new UIFrameViewAssets();

        /// <summary>
        /// UI层级接口方法
        /// </summary>
        private UIFrameOpenLayer m_UIOpenLayers = new UIFrameOpenLayer();
        
        /// <summary>
        /// 当前临时隐藏的UI
        /// </summary>
        private Dictionary<string,List<UIDisplay>> m_TampHideUIs = new();

        /// <summary>
        /// 预加载资源
        /// </summary>
        public bool PreLoad(string assetsName,ref UIDisplay UI)=> uiAssets.LoadUI(assetsName, ref UI);
        
        /// <summary>
        /// 显示UI
        /// </summary>
        /// <param name="UI">UI对象</param>
        public void DisplayUI(ref UIDisplay UI)
        {                
            UI.gameObject.SetActive(true);
            m_UIOpenLayers.SetUILayer(UI);
            
            if (UI.showType == ShowType.Focussing)
            {
                if (m_UIOpenLayers.GetFocussingList(out var recordTampHideUIs))
                {
                    foreach (var ui in recordTampHideUIs)
                        ui.gameObject.SetActive(false);
                    m_TampHideUIs[UI.name] = recordTampHideUIs;
                }
            }
        }

        /// <summary>
        /// 移除UI
        /// </summary>
        /// <param name="UI">需要隐藏的UI对象</param>
        /// <param name="quequeCallBack">依次移除队列中的UI的回调</param>
        /// <returns></returns>
        public void RemoveUI(UIDisplay UI, Action quequeCallBack=default)
        {
            UI.gameObject.SetActive(false);
            HDMUI.Runtime.FrameStore.TryRemoveToStore(UI, (queueUI) =>
            {
                quequeCallBack?.Invoke();
                queueUI.gameObject.SetActive(false);
            });
            if (UI.showType == ShowType.Focussing)
                RestoreUI(UI.name);
            m_UIOpenLayers.SetWrapperWall();
        }

        /// <summary>
        /// 销毁UI
        /// </summary>
        /// <param name="UI">需要销毁的UI对象</param>
        /// <param name="quequeCallBack">依次销毁队列中的UI的回调</param>
        /// <returns></returns>
        public void ReleaseUIAeeets(UIDisplay UI,Action quequeCallBack=default)
        {
            UI.gameObject.SetActive(false);
            uiAssets.DestroyUI(UI.name,UI);
            HDMUI.Runtime.FrameStore.TryDestroyToStore(UI, (queueUI) =>
            {
                queueUI.gameObject.SetActive(false);
                quequeCallBack?.Invoke();
                uiAssets.DestroyUI(queueUI.name, queueUI);
            });
            if (UI.showType == ShowType.Focussing)
                RestoreUI(UI.name);
            m_UIOpenLayers.SetWrapperWall();
        }

        /// <summary>
        /// 恢复临时隐藏的UI
        /// </summary>
        /// <param name="uiName">引起其它UI隐藏的UI名称</param>
        public void RestoreUI(string uiName)
        {
            if (m_TampHideUIs.TryGetValue(uiName,out var recordTampHideUIs))
            {
                foreach (var uiItems in recordTampHideUIs)
                {
                    if (uiItems)
                        uiItems?.gameObject.SetActive(true);
                }
            }
        }

    }
}
